package com.yikaiye.android.yikaiye.utils;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.application.MyApplication;

/**
 * Toast工具类
 */
public final class ToastHelper {

    /**
     * 最常用的提示文本
     */
    public static void show(String message) {
        Toast toast = new Toast(MyApplication.getInstance());
        TextView view = new TextView(MyApplication.getInstance());
        view.setBackgroundResource(R.drawable.shape_b3000000_4dp);
        view.setTextColor(Color.WHITE);
        view.setText(message + "");
        view.setGravity(Gravity.CENTER);
        view.setPadding(50, 20, 50, 20);
        toast.setGravity(Gravity.CENTER, 0, 40);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }

    public static void show(Context context, String message) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.toast_layout, null);
        TextView textView = layout.findViewById(R.id.text);
        textView.setText(message);
        Toast toast = new Toast(MyApplication.getInstance());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(layout);
        toast.show();
    }

    /**
     * 自定义Toast样式
     *
     * @param context 上下文
     * @param message 消息
     * @param icon    图标
     */
    public static void showTextAndImage(Context context, String message, int icon) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.toast_layout, null);
        ImageView imageView = layout.findViewById(R.id.icon);
        TextView textView = layout.findViewById(R.id.text);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(icon);
        textView.setText(message);
        Toast toast = new Toast(MyApplication.getInstance());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setView(layout);
        toast.show();
    }
}
